package com.apesay.service.auth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apesay.exception.ServiceException;
import com.apesay.service.base.ClientService;
import com.apesay.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RoleService {
	@Autowired
	private ClientService clientService;
	public JSONObject updateRole(String id,JSONObject object)  {
		JSONObject roleObject=new JSONObject();
		JSONArray jsonArray = object.getJSONArray("menus");
		roleObject.put("roleName", object.getString("roleName"));
		roleObject.put("permissions", object.getJSONArray("permissions"));
		JSONArray array = new JSONArray();
		for (Object object2 : jsonArray) {
			String oid = (String) object2;
			JSONObject query=new JSONObject();
			query.put(Constant.OBJECTID, oid);
			JSONObject find = clientService.find(query.toJSONString(), "{}", "{}",Constant.model_backend_menu, 0, 1);
			if (find.getJSONObject(Constant.RESULT).getIntValue("count")!=0) {
				array.add(oid);
			}
		}
		roleObject.put("menus", array);
		JSONObject saveObject= clientService.update(Constant.model_backend_role,id, roleObject);
		JSONArray userArray=object.getJSONArray("accounts");
		for(int i=0;i<userArray.size();i++){
			JSONObject userFetchObject= clientService.getObjectById(Constant.model_backend_account,userArray.getString(i));
			JSONObject userObject=userFetchObject.getJSONObject(Constant.RESULT);
			if(userObject.getJSONArray("roles")==null){
				JSONArray roleArray=new JSONArray();
				roleArray.add(saveObject.getJSONObject(Constant.RESULT).getString(Constant.OBJECTID));
				userObject.put("roles", roleArray);
			}else{
				if(!userObject.getJSONArray("roles").contains(saveObject.getJSONObject(Constant.RESULT).getString(Constant.OBJECTID))){
					userObject.getJSONArray("roles").add(saveObject.getJSONObject(Constant.RESULT).getString(Constant.OBJECTID));
				}
			}
			clientService.update(Constant.model_backend_account,userObject.getString(Constant.OBJECTID), userObject);
		}
		
		return saveObject;
	}
	public JSONObject createRole(JSONObject object) {
		JSONObject roleObject=new JSONObject();
		roleObject.put("roleName", object.getString("roleName"));
		roleObject.put("permissions", object.getJSONArray("permissions"));
		JSONObject saveObject= clientService.add(Constant.model_backend_role,roleObject);
		JSONArray userArray=object.getJSONArray("accounts");
		for(int i=0;i<userArray.size();i++){
			JSONObject userFetchObject= clientService.getObjectById(Constant.model_backend_account,userArray.getString(i));
			JSONObject userObject=userFetchObject.getJSONObject(Constant.RESULT);
			if(userObject.getJSONArray("roles")==null){
				JSONArray roleArray=new JSONArray();
				roleArray.add(saveObject.getJSONObject(Constant.RESULT).getString(Constant.OBJECTID));
				userObject.put("roles", roleArray);
			}else{
				userObject.getJSONArray("roles").add(saveObject.getJSONObject(Constant.RESULT).getString(Constant.OBJECTID));
			}
			clientService.update(Constant.model_backend_account,userObject.getString(Constant.OBJECTID), userObject);
		}
		
		return saveObject;
	}
	
	public JSONArray getRolesByUserId(String userId) {
		JSONObject where=new JSONObject();
		where.put("username", userId);
		JSONObject userObject= clientService.find(where.toJSONString(), "{}", "{}",Constant.model_backend_account, 0, 1);
		JSONArray result=new JSONArray();
		if(userObject!=null&&userObject.getJSONObject(Constant.RESULT)!=null&&userObject.getJSONObject(Constant.RESULT).getJSONArray(Constant.RESULTS)!=null){
			if(userObject.getJSONObject(Constant.RESULT).getJSONArray(Constant.RESULTS).size()>0){
				JSONObject user=userObject.getJSONObject(Constant.RESULT).getJSONArray(Constant.RESULTS).getJSONObject(0);
				if(user.getJSONArray("roles")!=null){
					result.addAll(user.getJSONArray("roles"));
				}
			}
		}
		return result;
	}
	public JSONArray getAllRolesByPage(int pageIndex,int pageSize) {
		JSONObject rolesObject= clientService.find("{}", "{}", "{}",Constant.model_backend_role, pageIndex*pageSize,pageSize);
		JSONArray roleArray=rolesObject.getJSONArray(Constant.RESULT);
		if(roleArray!=null&&roleArray.size()>0){
			return roleArray;
		}
		return new JSONArray();
	}
}
